package com.ckerchina.admin.ckadmin.dao;

import java.util.Date;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ckerchina.admin.ckadmin.domain.ShareDetail;

@Repository
public class ShareDetailDAO extends AbstractDao<ShareDetail, Long>{

	@Transactional
	public void persist(ShareDetail shareDetail) {
		this.entityManager.persist(shareDetail);
	}

	@Transactional
	public ShareDetail merge(ShareDetail shareDetail) {
		ShareDetail merged = this.entityManager.merge(shareDetail);
		this.entityManager.flush();
		return merged;
	}
	
	public long getShareDetailsCountByType(int type,long userId,Date beginTime,Date endTime){
		String sql = "SELECT count(1) FROM ShareDetail sd WHERE sd.type=? AND sd.userId=?";
		if(beginTime!=null){
			sql += " AND sd.createTime BETWEEN ? AND  ?";
		}
		
		TypedQuery<Long> query = entityManager.createQuery(sql,Long.class).setParameter(1, type).setParameter(2, userId);
		if(beginTime!=null){
			query.setParameter(3, beginTime).setParameter(4, endTime);
		}
		return query.getSingleResult();
	}
	
	

}
